﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using YArchitech.LIB;
using libxl;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.Office.Interop.Excel;

namespace YArchitech.Plumbing
{
	public class ExportExcel
	{
		public ExportExcel(InBuildSystem inBuildSystem, OptionData optionData)
		{
			this.buildSystem = inBuildSystem;
			this.option = optionData;
		}

		public bool WriteIntoExcel(BackgroundWorker bkWorker = null)
		{
			bool result = true;
			this.backWorker = bkWorker;
			try
			{
				this.SetBkWorInfo(0, "启动Excel...");
				this.excelApplication = (Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
				this.SetBkWorInfo(10, "启动Excel, 创建工作簿...");
				this.workbook = this.excelApplication.Workbooks.Add(1);
				this.workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                //if (ExportExcel.<>o__1.<>p__0 == null)
                //{
                //    ExportExcel.<>o__1.<>p__0 = CallSite<Func<CallSite, object, Worksheet>>.Create(Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(Worksheet), typeof(ExportExcel)));
                //}
				//this.sheetSystemCalc = ExportExcel.<>o__1.<>p__0.Target(ExportExcel.<>o__1.<>p__0, this.workbook.Sheets[1]);
				this.sheetSystemCalc.Name = "分户热计量水力计算表";
				int num = 1;
				this.SetBkWorInfo(2, "开始写计算数据...");
				this.SetBkWorInfo(2, "正在输出标题...");
				this.WriteCalcHeader(ref num);
				this.SetBkWorInfo(11, "正在输出系统设置信息...");
				this.WriteOption(ref num);
				this.SetBkWorInfo(this.buildSystem.RiserPipeNum * 2, "正在输干管水力计算表信息...");
				this.WriteMainPipe(ref num, "总供回水干管水力计算表", this.buildSystem.SupplyTmPipe, this.buildSystem.ReturnTmPipe);
				int num2 = 0;
				foreach (ShareRPipeSystem shareRPipeSystem in this.buildSystem.SrpSystems)
				{
					foreach (HYPipeData hypipeData in shareRPipeSystem.SupplyPipes)
					{
						num2++;
					}
				}
				this.SetBkWorInfo(num2, "正在输立管水力计算表信息...");
				this.WriteRiserPipe(ref num);
				num2 = 0;
				foreach (ShareRPipeSystem shareRPipeSystem2 in this.buildSystem.SrpSystems)
				{
					foreach (FloorSystem floorSystem in shareRPipeSystem2.FloorSystems)
					{
						foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
						{
							foreach (CirclePipe circlePipe in indoorSystem.CirclePipes)
							{
								num2++;
							}
						}
					}
				}
				this.SetBkWorInfo(num2, "正在输环路水力计算表信息...");
				this.WriteCirclePipe(ref num);
				num += 2;
				this.WriteCalcData(num);
				this.WriteCoefficient();
				this.excelApplication.Visible = true;
				GC.Collect();
			}
			catch (System.Exception)
			{
				throw;
			}
			return result;
		}

		private void WriteCoefficient()
		{
			this.SetBkWorInfo(0, "正在创建局阻信息表...");
            //if (ExportExcel.<>o__2.<>p__0 == null)
            //{
            //    ExportExcel.<>o__2.<>p__0 = CallSite<Func<CallSite, object, Worksheet>>.Create(Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(Worksheet), typeof(ExportExcel)));
            //}
            //this.sheetCoefficient = ExportExcel.<>o__2.<>p__0.Target(ExportExcel.<>o__2.<>p__0, this.workbook.Sheets[2]);
			this.sheetCoefficient.Name = "高级设置";
			int num = 1;
			this.SetBkWorInfo(2, "正在写局阻信息表标题...");
			this.WriteCoeffHeader(ref num);
			this.SetBkWorInfo(2, "正在写局阻系统设置信息...");
			this.WriteCoefOption(ref num);
			this.SetBkWorInfo(this.buildSystem.RiserPipeNum * 2, "正在输干管局阻表信息...");
			this.WriteMainPipeCoef(ref num);
			int num2 = 0;
			foreach (ShareRPipeSystem shareRPipeSystem in this.buildSystem.SrpSystems)
			{
				foreach (HYPipeData hypipeData in shareRPipeSystem.SupplyPipes)
				{
					num2++;
				}
			}
			this.SetBkWorInfo(num2, "正在输立管局阻表信息...");
			this.WriteRiserPipeCoef(ref num);
			num2 = 0;
			foreach (ShareRPipeSystem shareRPipeSystem2 in this.buildSystem.SrpSystems)
			{
				foreach (FloorSystem floorSystem in shareRPipeSystem2.FloorSystems)
				{
					foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
					{
						foreach (CirclePipe circlePipe in indoorSystem.CirclePipes)
						{
							num2++;
						}
					}
				}
			}
			this.SetBkWorInfo(num2, "正在输户内局阻表信息...");
			this.WriteIndoorCoef(ref num);
		}

		private void WriteMainPipeCoef(ref int rowIndex)
		{
			int num = rowIndex;
			rowIndex += 2;
			int num2 = 1;
			for (int i = 0; i < this.buildSystem.SupplyTmPipe.Count; i++)
			{
				int num3 = this.WritePipeCoef(rowIndex, "供水" + (i + 1), this.buildSystem.SupplyTmPipe[i]);
				if (num3 > num2)
				{
					num2 = num3;
				}
				rowIndex += 2;
			}
			for (int j = 0; j < this.buildSystem.ReturnTmPipe.Count; j++)
			{
				int num4 = this.WritePipeCoef(rowIndex, "回水" + (j + 1), this.buildSystem.ReturnTmPipe[j]);
				if (num4 > num2)
				{
					num2 = num4;
				}
				rowIndex += 2;
			}
			this.WriteText(this.sheetCoefficient, num, 1, num2, "总供回水干管水力计算表", ExportExcel.Align.Center);
			this.WriteText(this.sheetCoefficient, num + 1, 1, 1, "编号", ExportExcel.Align.Center);
			this.WriteText(this.sheetCoefficient, num + 1, 2, 2, "局阻系数", ExportExcel.Align.Center);
			if (num2 > 2)
			{
				this.WriteText(this.sheetCoefficient, num + 1, 3, num2, "阀件", ExportExcel.Align.Center);
			}
			this.AddBorder(this.sheetCoefficient, num + 1, 1, rowIndex - 1, num2);
			rowIndex += 2;
		}

		private void WriteRiserPipeCoef(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				int num = rowIndex;
				rowIndex += 2;
				int num2 = 1;
				for (int j = 0; j < shareRPipeSystem.SupplyPipes.Count; j++)
				{
					int num3 = this.WritePipeCoef(rowIndex, "供水" + (j + 1), shareRPipeSystem.SupplyPipes[j]);
					if (num3 > num2)
					{
						num2 = num3;
					}
					rowIndex += 2;
				}
				for (int k = 0; k < shareRPipeSystem.ReturnPipes.Count; k++)
				{
					int num4 = this.WritePipeCoef(rowIndex, "回水" + (k + 1), shareRPipeSystem.ReturnPipes[k]);
					if (num4 > num2)
					{
						num2 = num4;
					}
					rowIndex += 2;
				}
				string text = string.Format("共用立管[{0}]水力计算表", i + 1);
				this.WriteText(this.sheetCoefficient, num, 1, num2, text, ExportExcel.Align.Center);
				this.WriteText(this.sheetCoefficient, num + 1, 1, 1, "编号", ExportExcel.Align.Center);
				this.WriteText(this.sheetCoefficient, num + 1, 2, 2, "局阻系数", ExportExcel.Align.Center);
				if (num2 > 2)
				{
					this.WriteText(this.sheetCoefficient, num + 1, 3, num2, "阀件", ExportExcel.Align.Center);
				}
				this.AddBorder(this.sheetCoefficient, num + 1, 1, rowIndex - 1, num2);
				rowIndex += 2;
			}
		}

		private void WriteIndoorCoef(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				for (int j = 0; j < shareRPipeSystem.FloorSystems.Count; j++)
				{
					FloorSystem floorSystem = shareRPipeSystem.FloorSystems[j];
					for (int k = 0; k < floorSystem.IndoorSystems.Count; k++)
					{
						IndoorSystem indoorSystem = floorSystem.IndoorSystems[k];
						int num = rowIndex;
						rowIndex += 2;
						int num2 = 1;
						if (indoorSystem.CircleNum > 1)
						{
							int num3 = this.WritePipeCoef(rowIndex, "供水" + 1, indoorSystem.SupplyMPipe);
							if (num3 > num2)
							{
								num2 = num3;
							}
							rowIndex += 2;
							num3 = this.WritePipeCoef(rowIndex, "回水" + 1, indoorSystem.ReturnMPipe);
							if (num3 > num2)
							{
								num2 = num3;
							}
							rowIndex += 2;
						}
						for (int l = 0; l < indoorSystem.CirclePipes.Count; l++)
						{
							int num4 = this.WritePipeCoef(rowIndex, "环路" + (l + 1), indoorSystem.ReturnMPipe);
							if (num4 > num2)
							{
								num2 = num4;
							}
							rowIndex += 2;
						}
						string text = string.Format("共用立管[{0}]楼层[{1}]户内系统[{2}]水力计算表", i + 1, j + 1, k + 1);
						if (num2 > 3)
						{
							this.WriteText(this.sheetCoefficient, num, 1, num2, text, ExportExcel.Align.Center);
						}
						else
						{
							this.WriteText(this.sheetCoefficient, num, 1, 4, text, ExportExcel.Align.Center);
						}
						this.WriteText(this.sheetCoefficient, num + 1, 1, 1, "编号", ExportExcel.Align.Center);
						this.WriteText(this.sheetCoefficient, num + 1, 2, 2, "局阻系数", ExportExcel.Align.Center);
						if (num2 > 2)
						{
							this.WriteText(this.sheetCoefficient, num + 1, 3, num2, "阀件", ExportExcel.Align.Center);
						}
						this.AddBorder(this.sheetCoefficient, num + 1, 1, rowIndex - 1, num2);
						rowIndex += 2;
					}
				}
			}
		}

		private void WriteCalcHeader(ref int rowIndex)
		{
			this.WriteText(this.sheetSystemCalc, rowIndex, 1, 13, "分户热计量水力计算表", ExportExcel.Align.Center);
			Microsoft.Office.Interop.Excel.Range range = this.sheetSystemCalc.get_Range(this.ToCellPosition(rowIndex, 1), this.ToCellPosition(rowIndex, 13));
			range.Font.Size = 11;
			range.Font.Name = "黑体";
			rowIndex += 2;
		}

		private void WriteCoeffHeader(ref int rowIndex)
		{
			this.WriteText(this.sheetCoefficient, rowIndex, 1, 5, "高级设置计算数据", ExportExcel.Align.Center);
			Microsoft.Office.Interop.Excel.Range range = this.sheetCoefficient.get_Range(this.ToCellPosition(rowIndex, 1), this.ToCellPosition(rowIndex, 5));
			range.Font.Size = 11;
			range.Font.Name = "黑体";
			rowIndex += 2;
		}

		private void WriteOption(ref int rowIndex)
		{
			int startRow = rowIndex;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "设计软件:", ExportExcel.Align.Right);
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, "盈建科机电 for Revit 2014-2016", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "计算时间:", ExportExcel.Align.Right);
			string text = DateTime.Now.ToUniversalTime().ToString();
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, text, ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "室内供回干管形式:", ExportExcel.Align.Right);
			string text2 = "同程";
			if (this.option.MainPipeInter != 1)
			{
				text2 = "异程";
			}
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, text2, ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "室内公用立管形式:", ExportExcel.Align.Right);
			if (this.option.RiserPipeInter != 1)
			{
				text2 = "异程";
			}
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, text2, ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "采暖形式:", ExportExcel.Align.Right);
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, "地热盘管采暖", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "供水温度:", ExportExcel.Align.Right);
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, this.option.TemperSupply + "℃", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "回水温度:", ExportExcel.Align.Right);
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, this.option.TemperReturn + "℃", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "系统总负荷:", ExportExcel.Align.Right);
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, this.buildSystem.TotalLoad + "W", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "系统总流量:", ExportExcel.Align.Right);
			string str = StringTools.DoubleToString(this.buildSystem.TotalFlow, 0);
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, str + "kg/h", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 4, 6, "系统总压降:", ExportExcel.Align.Right);
			string str2 = StringTools.DoubleToString(this.buildSystem.TotalResistance, 2);
			this.WriteText(this.sheetSystemCalc, rowIndex, 7, 9, str2 + "Pa", ExportExcel.Align.Left);
			this.AddBorder(this.sheetSystemCalc, startRow, 4, rowIndex, 9);
			rowIndex += 2;
		}

		private void WriteCoefOption(ref int rowIndex)
		{
			int startRow = rowIndex;
			double num = (this.option.TemperSupply + this.option.TemperReturn) / 2.0;
			string text = "无";
			if (this.option.DPValvePosition == 1)
			{
				text = "入户处";
			}
			else if (this.option.DPValvePosition == 2)
			{
				text = "公用立管";
			}
			this.WriteText(this.sheetCoefficient, rowIndex, 1, 3, "平均水温:", ExportExcel.Align.Right);
			this.WriteText(this.sheetCoefficient, rowIndex, 4, 5, num + "℃", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetCoefficient, rowIndex, 1, 3, "入户装置局部系数:", ExportExcel.Align.Right);
			this.WriteText(this.sheetCoefficient, rowIndex, 4, 5, this.option.ResistanceLocal.ToString(), ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetCoefficient, rowIndex, 1, 3, "入户装置局部阻力:", ExportExcel.Align.Right);
			this.WriteText(this.sheetCoefficient, rowIndex, 4, 5, this.option.Resistance + "kPa", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetCoefficient, rowIndex, 1, 3, "压差控制阀位置:", ExportExcel.Align.Right);
			this.WriteText(this.sheetCoefficient, rowIndex, 4, 5, text, ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetCoefficient, rowIndex, 1, 3, "压差控制阀正常工作最小压力差:", ExportExcel.Align.Right);
			this.WriteText(this.sheetCoefficient, rowIndex, 4, 5, this.option.DiffDPValve + "kPa", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetCoefficient, rowIndex, 1, 3, "温控阀正常工作最小压力差:", ExportExcel.Align.Right);
			this.WriteText(this.sheetCoefficient, rowIndex, 4, 5, this.option.DiffTCValve + "kPa", ExportExcel.Align.Left);
			this.AddBorder(this.sheetCoefficient, startRow, 1, rowIndex, 5);
			rowIndex += 3;
		}

		private void WriteMainPipe(ref int rowIndex, string tableName, IList<HYPipeData> supplyPipe, IList<HYPipeData> returnPipe)
		{
			this.WriteText(this.sheetSystemCalc, rowIndex, 1, 12, tableName, ExportExcel.Align.Center);
			rowIndex++;
			int startRow = rowIndex;
			int num = 1;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "编号", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "负荷(W)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "G(kg/h)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "公称直径", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "长度(m)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "V(m/s)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "R(Pa/m)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "Pd(Pa)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "Σξ", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "ΔPy(Pa)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "ΔPj(Pa)", ExportExcel.Align.Center);
			num++;
			this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "ΔP(Pa)", ExportExcel.Align.Center);
			rowIndex++;
			this.WritePipe(ref rowIndex, "供水", supplyPipe);
			this.WritePipe(ref rowIndex, "回水", returnPipe);
			this.AddBorder(this.sheetSystemCalc, startRow, 1, rowIndex - 1, 12);
			rowIndex++;
		}

		private void WritePipe(ref int rowIndex, string identName, IList<HYPipeData> listPipe)
		{
			for (int i = 0; i < listPipe.Count; i++)
			{
				HYPipeData hypipeData = listPipe[i];
				int num = 1;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, identName + (i + 1), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.Load, 0), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.Flow, 0), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString((double)hypipeData.Diameter, 0), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.Length, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.Speed, 3), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.ResistanceFrict, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.PressDynamic, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.ResCoefLocal, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.DragForce, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.ResistanceLocal, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(hypipeData.Resistance, 2), ExportExcel.Align.Center);
				rowIndex++;
			}
		}

		private int WritePipeCoef(int rowIndex, string identName, HYPipeData pipeData)
		{
			int num = 1;
			this.WriteText(this.sheetCoefficient, rowIndex, rowIndex + 1, num, num, identName);
			num++;
			this.WriteText(this.sheetCoefficient, rowIndex, rowIndex + 1, num, num, pipeData.ResCoefLocal.ToString());
			for (int i = 0; i < pipeData.ValvesList.Count; i++)
			{
				ValveAndFitting valveAndFitting = pipeData.ValvesList[i];
				num++;
				this.WriteText(this.sheetCoefficient, rowIndex, num, num, valveAndFitting.VFName, ExportExcel.Align.Center);
				if (valveAndFitting.VFName == ValveString.散热器.ToString() || valveAndFitting.VFName == ValveString.其他.ToString())
				{
					this.WriteText(this.sheetCoefficient, rowIndex + 1, num, num, valveAndFitting.Value.ToString(), ExportExcel.Align.Center);
				}
				else
				{
					this.WriteText(this.sheetCoefficient, rowIndex + 1, num, num, valveAndFitting.Value + "个", ExportExcel.Align.Center);
				}
			}
			return num;
		}

		private void WriteRiserPipe(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				string tableName = string.Format("共用立管[{0}]水力计算表", i + 1);
				this.WriteMainPipe(ref rowIndex, tableName, shareRPipeSystem.SupplyPipes, shareRPipeSystem.ReturnPipes);
			}
		}

		private void WriteCalcData(int rowIndex)
		{
			int startRow = rowIndex;
			this.WriteText(this.sheetSystemCalc, rowIndex, 1, 2, "设计软件:", ExportExcel.Align.Right);
			this.WriteText(this.sheetSystemCalc, rowIndex, 3, 5, "盈建科机电 for Revit 2014-2016", ExportExcel.Align.Left);
			rowIndex++;
			this.WriteText(this.sheetSystemCalc, rowIndex, 1, 2, "计算时间:", ExportExcel.Align.Right);
			string text = DateTime.Now.ToUniversalTime().ToString();
			this.WriteText(this.sheetSystemCalc, rowIndex, 3, 5, text, ExportExcel.Align.Left);
			this.AddBorder(this.sheetSystemCalc, startRow, 1, rowIndex, 5);
		}

		private void WriteCirclePipe(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				for (int j = 0; j < shareRPipeSystem.FloorSystems.Count; j++)
				{
					FloorSystem floorSystem = shareRPipeSystem.FloorSystems[j];
					for (int k = 0; k < floorSystem.IndoorNum; k++)
					{
						IndoorSystem indoorSystem = floorSystem.IndoorSystems[k];
						string text = string.Format("共用立管[{0}]楼层[{1}]户内系统[{2}]水力计算表", i + 1, j + 1, k + 1);
						this.WriteText(this.sheetSystemCalc, rowIndex, 1, 13, text, ExportExcel.Align.Center);
						rowIndex++;
						int startRow = rowIndex;
						int num = 1;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "编号", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "负荷(W)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "G(kg/h)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "公称直径", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "长度(m)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "V(m/s)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "R(Pa/m)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "Pd(Pa)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "Σξ", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "ΔPy(Pa)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "ΔPj(Pa)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "ΔP(Pa)", ExportExcel.Align.Center);
						num++;
						this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "不平衡率%", ExportExcel.Align.Center);
						rowIndex++;
						this.WriteCircle(ref rowIndex, indoorSystem.CirclePipes);
						this.AddBorder(this.sheetSystemCalc, startRow, 1, rowIndex - 1, 13);
						rowIndex++;
					}
				}
			}
		}

		private void WriteCircle(ref int rowIndex, IList<CirclePipe> listCircle)
		{
			for (int i = 0; i < listCircle.Count; i++)
			{
				CirclePipe circlePipe = listCircle[i];
				int num = 1;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "环路" + (i + 1), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.Load, 0), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.Flow, 0), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, "De" + circlePipe.PipeData.Diameter, ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.PipeData.Length, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.PipeData.Speed, 3), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.PipeData.ResistanceFrict, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.PipeData.PressDynamic, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.ResCoefLocal, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.PipeData.DragForce, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.PipeData.ResistanceLocal, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.PipeData.Resistance, 2), ExportExcel.Align.Center);
				num++;
				this.WriteText(this.sheetSystemCalc, rowIndex, num, num, StringTools.DoubleToString(circlePipe.RateUnbalance, 2), ExportExcel.Align.Center);
				rowIndex++;
			}
		}

        private void WriteText(Microsoft.Office.Interop.Excel.Worksheet workSheet, int rowIndex, int scIndex, int ecIndex, string text, ExportExcel.Align align = ExportExcel.Align.Center)
		{
			workSheet.Cells[rowIndex, scIndex] = text;
			Microsoft.Office.Interop.Excel.Range range = workSheet.get_Range(this.ToCellPosition(rowIndex, scIndex), this.ToCellPosition(rowIndex, ecIndex));
			range.MergeCells = true;
			range.VerticalAlignment = XlVAlign.xlVAlignCenter;
			range.Font.Size = 10;
			range.Font.Name = "宋体";
			if (align == ExportExcel.Align.Center)
			{
				range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
				return;
			}
			if (align == ExportExcel.Align.Left)
			{
				range.HorizontalAlignment = XlHAlign.xlHAlignLeft;
				return;
			}
			range.HorizontalAlignment = XlHAlign.xlHAlignRight;
		}

		private void WriteText(Worksheet workSheet, int rowIndex, int endRowIndex, int scIndex, int ecIndex, string text)
		{
			workSheet.Cells[rowIndex, scIndex] = text;
			Microsoft.Office.Interop.Excel.Range range = workSheet.get_Range(this.ToCellPosition(rowIndex, scIndex), this.ToCellPosition(endRowIndex, ecIndex));
			range.MergeCells = true;
			range.VerticalAlignment = XlVAlign.xlVAlignCenter;
			range.Font.Size = 10;
			range.Font.Name = "宋体";
			range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
		}

		private void AddBorder(Worksheet workSheet, int startRow, int startCol, int endRow, int endCol)
		{
			Microsoft.Office.Interop.Excel.Range range = workSheet.get_Range(this.ToCellPosition(startRow, startCol), this.ToCellPosition(endRow, endCol));
			Borders borders = range.Borders;
			Border border = borders.get_Item(XlBordersIndex.xlEdgeLeft);
			borders.LineStyle = XlLineStyle.xlContinuous;
			borders.Weight = XlBorderWeight.xlThin;
			borders.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
			range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
			Border border2 = borders.get_Item(XlBordersIndex.xlEdgeTop);
			borders.LineStyle = XlLineStyle.xlContinuous;
			borders.Weight = XlBorderWeight.xlThin;
			borders.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
			range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
			Border border3 = borders.get_Item(XlBordersIndex.xlEdgeBottom);
			borders.LineStyle = XlLineStyle.xlContinuous;
			borders.Weight = XlBorderWeight.xlThin;
			borders.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
			range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
			Border border4 = borders.get_Item(XlBordersIndex.xlEdgeRight);
			borders.LineStyle = XlLineStyle.xlContinuous;
			borders.Weight = XlBorderWeight.xlThin;
			borders.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
			range.BorderAround(XlLineStyle.xlDouble, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
			Border border5 = borders.get_Item(XlBordersIndex.xlInsideVertical);
			borders.LineStyle = XlLineStyle.xlContinuous;
			borders.Weight = XlBorderWeight.xlThin;
			borders.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
			range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
			Border border6 = borders.get_Item(XlBordersIndex.xlInsideHorizontal);
			borders.LineStyle = XlLineStyle.xlContinuous;
			borders.Weight = XlBorderWeight.xlThin;
			borders.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
			range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
		}

		private string ToCellPosition(int rowIndex, int columnIndex)
		{
			return string.Format("{0}{1}", this.GetColumnName(columnIndex), rowIndex);
		}

		private string GetColumnName(int columnIndex)
		{
			columnIndex--;
			if (columnIndex < 26)
			{
				char c = (char)(columnIndex + 65);
				return string.Format("{0}", c);
			}
			if (columnIndex > 51)
			{
				throw new System.Exception("列数太多了，暂时不支持");
			}
			columnIndex -= 26;
			return string.Format("{0}{1}", "A", (char)(columnIndex + 65));
		}

		private void SetBkWorInfo(int step, string messge = "")
		{
			if (this.backWorker == null)
			{
				return;
			}
			this.rate += step;
			if (this.rate > 100)
			{
				this.rate = 100;
			}
			if (string.IsNullOrEmpty(messge))
			{
				this.backWorker.ReportProgress(this.rate);
				return;
			}
			this.backWorker.ReportProgress(this.rate, messge);
		}

		public bool ExportIntoExcel(BackgroundWorker bkWorker = null)
		{
			bool result = true;
			this.backWorker = bkWorker;
			try
			{
				this.SetBkWorInfo(0, "启动Excel...");
				this.book = new BinBook();
				this.book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
				this.SetBkWorInfo(10, "启动Excel, 创建工作簿...");
				this.bkSheet = this.book.addSheet("分户热计量水力计算表");
				this.InitialLibxlFormat();
				int num = 0;
				this.SetBkWorInfo(2, "开始写计算数据...");
				this.SetBkWorInfo(2, "正在输出标题...");
				this.ExportHeader(ref num, this.bkSheet, 0, 13, "分户热计量水力计算表");
				this.SetBkWorInfo(11, "正在输出系统设置信息...");
				this.ExportOption(ref num);
				this.SetBkWorInfo(this.buildSystem.RiserPipeNum * 2, "正在输干管水力计算表信息...");
				this.ExportMainPipe(ref num, "总供回水干管水力计算表", this.buildSystem.SupplyTmPipe, this.buildSystem.ReturnTmPipe);
				int num2 = 0;
				foreach (ShareRPipeSystem shareRPipeSystem in this.buildSystem.SrpSystems)
				{
					foreach (HYPipeData hypipeData in shareRPipeSystem.SupplyPipes)
					{
						num2++;
					}
				}
				this.SetBkWorInfo(num2, "正在输立管水力计算表信息...");
				this.ExportRiserPipe(ref num);
				num2 = 0;
				foreach (ShareRPipeSystem shareRPipeSystem2 in this.buildSystem.SrpSystems)
				{
					foreach (FloorSystem floorSystem in shareRPipeSystem2.FloorSystems)
					{
						foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
						{
							foreach (CirclePipe circlePipe in indoorSystem.CirclePipes)
							{
								num2++;
							}
						}
					}
				}
				this.SetBkWorInfo(num2, "正在输环路水力计算表信息...");
				this.ExportCirclePipe(ref num);
				num += 2;
				this.ExportCalcData(num);
				this.ExportCoefficient();
				string text = "format";
				string saveExcelPath = this.GetSaveExcelPath(ref text);
				this.book.save(saveExcelPath);
				Process.Start(saveExcelPath);
			}
			catch (System.Exception)
			{
				throw;
			}
			return result;
		}

		private string GetSaveExcelPath(ref string intialPath)
		{
			string text = Path.Combine(Product.UserDataLocation, intialPath + ".xls");
			if (File.Exists(text))
			{
                //intialPath++;
				text = this.GetSaveExcelPath(ref intialPath);
			}
			return text;
		}

		private void ExportCoefficient()
		{
			this.SetBkWorInfo(0, "正在创建局阻信息表...");
			this.bkSheet = this.book.addSheet("高级设置");
			int num = 0;
			this.SetBkWorInfo(2, "正在写局阻信息表标题...");
			this.ExportHeader(ref num, this.bkSheet, 0, 4, "高级设置计算数据");
			this.SetBkWorInfo(2, "正在写局阻系统设置信息...");
			this.ExportCoefOption(ref num);
			this.SetBkWorInfo(this.buildSystem.RiserPipeNum * 2, "正在输干管局阻表信息...");
			this.ExportMainPipeCoef(ref num);
			int num2 = 0;
			foreach (ShareRPipeSystem shareRPipeSystem in this.buildSystem.SrpSystems)
			{
				foreach (HYPipeData hypipeData in shareRPipeSystem.SupplyPipes)
				{
					num2++;
				}
			}
			this.SetBkWorInfo(num2, "正在输立管局阻表信息...");
			this.ExportRiserPipeCoef(ref num);
			num2 = 0;
			foreach (ShareRPipeSystem shareRPipeSystem2 in this.buildSystem.SrpSystems)
			{
				foreach (FloorSystem floorSystem in shareRPipeSystem2.FloorSystems)
				{
					foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
					{
						foreach (CirclePipe circlePipe in indoorSystem.CirclePipes)
						{
							num2++;
						}
					}
				}
			}
			this.SetBkWorInfo(num2, "正在输户内局阻表信息...");
			this.ExportIndoorCoef(ref num);
		}

		private void ExportIndoorCoef(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				for (int j = 0; j < shareRPipeSystem.FloorSystems.Count; j++)
				{
					FloorSystem floorSystem = shareRPipeSystem.FloorSystems[j];
					for (int k = 0; k < floorSystem.IndoorSystems.Count; k++)
					{
						IndoorSystem indoorSystem = floorSystem.IndoorSystems[k];
						int num = rowIndex;
						rowIndex += 2;
						int num2 = 0;
						if (indoorSystem.CircleNum > 1)
						{
							int num3 = this.ExportPipeCoefNew(rowIndex, "供水" + 1, indoorSystem.SupplyMPipe);
							if (num3 > num2)
							{
								num2 = num3;
							}
							rowIndex += 2;
							num3 = this.ExportPipeCoefNew(rowIndex, "回水" + 1, indoorSystem.ReturnMPipe);
							if (num3 > num2)
							{
								num2 = num3;
							}
							rowIndex += 2;
						}
						for (int l = 0; l < indoorSystem.CirclePipes.Count; l++)
						{
							int num4 = this.ExportPipeCoefNew(rowIndex, "环路" + (l + 1), indoorSystem.CirclePipes[l].PipeData);
							if (num4 > num2)
							{
								num2 = num4;
							}
							rowIndex += 2;
						}
						string value = string.Format("共用立管[{0}]楼层[{1}]户内系统[{2}]水力计算表", i + 1, j + 1, k + 1);
						if (num2 > 2)
						{
							this.ExportTextNew(this.bkSheet, num, 0, num2, value, this.nameTableFormat);
						}
						else
						{
							this.ExportTextNew(this.bkSheet, num, 0, 3, value, this.nameTableFormat);
						}
						this.ExportTextNew(num + 1, 0, 0, "编号", this.bkSheet, this.tableFormat);
						this.ExportTextNew(num + 1, 1, 1, "局阻系数", this.bkSheet, this.tableFormat);
						if (num2 > 1)
						{
							this.ExportTextNew(num + 1, 2, num2, "阀件", this.bkSheet, this.tableFormat);
						}
						rowIndex += 2;
					}
				}
			}
		}

		private void ExportRiserPipeCoef(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				int num = rowIndex;
				rowIndex += 2;
				int num2 = 0;
				for (int j = 0; j < shareRPipeSystem.SupplyPipes.Count; j++)
				{
					int num3 = this.ExportPipeCoefNew(rowIndex, "供水" + (j + 1), shareRPipeSystem.SupplyPipes[j]);
					if (num3 > num2)
					{
						num2 = num3;
					}
					rowIndex += 2;
				}
				for (int k = 0; k < shareRPipeSystem.ReturnPipes.Count; k++)
				{
					int num4 = this.ExportPipeCoefNew(rowIndex, "回水" + (k + 1), shareRPipeSystem.ReturnPipes[k]);
					if (num4 > num2)
					{
						num2 = num4;
					}
					rowIndex += 2;
				}
				string value = string.Format("共用立管[{0}]水力计算表", i + 1);
				this.ExportText(this.bkSheet, num, 0, num2, value, this.nameTableFormat);
				this.ExportText(this.bkSheet, num + 1, 0, 0, "编号", this.tableFormat);
				this.ExportText(this.bkSheet, num + 1, 1, 1, "局阻系数", this.tableFormat);
				if (num2 > 1)
				{
					this.ExportText(this.bkSheet, num + 1, 2, num2, "阀件", this.tableFormat);
				}
				rowIndex += 2;
			}
		}

		private void ExportMainPipeCoef(ref int rowIndex)
		{
			int num = rowIndex;
			rowIndex += 2;
			int num2 = 0;
			for (int i = 0; i < this.buildSystem.SupplyTmPipe.Count; i++)
			{
				int num3 = this.ExportPipeCoef(rowIndex, "供水" + (i + 1), this.buildSystem.SupplyTmPipe[i]);
				if (num3 > num2)
				{
					num2 = num3;
				}
				rowIndex += 2;
			}
			for (int j = 0; j < this.buildSystem.ReturnTmPipe.Count; j++)
			{
				int num4 = this.ExportPipeCoef(rowIndex, "回水" + (j + 1), this.buildSystem.ReturnTmPipe[j]);
				if (num4 > num2)
				{
					num2 = num4;
				}
				rowIndex += 2;
			}
			this.ExportText(this.bkSheet, num, 0, num2, "总供回水干管水力计算表", this.nameTableFormat);
			this.ExportText(this.bkSheet, num + 1, 0, 0, "编号", this.tableFormat);
			this.ExportText(this.bkSheet, num + 1, 1, 1, "局阻系数", this.tableFormat);
			if (num2 > 1)
			{
				this.ExportText(this.bkSheet, num + 1, 2, num2, "阀件", this.tableFormat);
			}
			rowIndex += 2;
		}

		private int ExportPipeCoef(int rowIndex, string identName, HYPipeData pipeData)
		{
			int num = 0;
			this.ExportText(this.bkSheet, identName, rowIndex, rowIndex + 1, num, this.tableFormat);
			num++;
			this.ExportText(this.bkSheet, pipeData.ResCoefLocal.ToString(), rowIndex, rowIndex + 1, num, this.tableFormat);
			for (int i = 0; i < pipeData.ValvesList.Count; i++)
			{
				ValveAndFitting valveAndFitting = pipeData.ValvesList[i];
				num++;
				this.ExportTextNew(this.bkSheet, rowIndex, num, num, valveAndFitting.VFName, this.tableFormat);
				if (valveAndFitting.VFName == ValveString.散热器.ToString() || valveAndFitting.VFName == ValveString.其他.ToString())
				{
					this.ExportTextNew(this.bkSheet, rowIndex + 1, num, num, valveAndFitting.Value.ToString(), this.tableFormat);
				}
				else
				{
					this.ExportTextNew(this.bkSheet, rowIndex + 1, num, num, valveAndFitting.Value + "个", this.tableFormat);
				}
			}
			return num;
		}

		private int ExportPipeCoefNew(int rowIndex, string identName, HYPipeData pipeData)
		{
			int num = 0;
			this.ExportTextNew(this.bkSheet, identName, rowIndex, rowIndex + 1, num, this.tableFormat);
			num++;
			this.ExportTextNew(this.bkSheet, pipeData.ResCoefLocal.ToString(), rowIndex, rowIndex + 1, num, this.tableFormat);
			if (pipeData.ValvesList != null && pipeData.ValvesList.Count > 0)
			{
				for (int i = 0; i < pipeData.ValvesList.Count; i++)
				{
					ValveAndFitting valveAndFitting = pipeData.ValvesList[i];
					num++;
					this.ExportTextNew(this.bkSheet, rowIndex, num, num, valveAndFitting.VFName, this.tableFormat);
					if (valveAndFitting.VFName == ValveString.散热器.ToString() || valveAndFitting.VFName == ValveString.其他.ToString())
					{
						this.ExportTextNew(this.bkSheet, rowIndex + 1, num, num, valveAndFitting.Value.ToString(), this.tableFormat);
					}
					else
					{
						this.ExportTextNew(this.bkSheet, rowIndex + 1, num, num, valveAndFitting.Value + "个", this.tableFormat);
					}
				}
			}
			return num;
		}

		private void ExportCoefOption(ref int rowIndex)
		{
			double num = (this.option.TemperSupply + this.option.TemperReturn) / 2.0;
			string value = "无";
			if (this.option.DPValvePosition == 1)
			{
				value = "入户处";
			}
			else if (this.option.DPValvePosition == 2)
			{
				value = "公用立管";
			}
			string value2 = "d";
			if (this.option.MaterialInternal == 2)
			{
				value2 = "φ";
			}
			else if (this.option.MaterialInternal == 3)
			{
				value2 = "DN";
			}
			this.ExportText(this.bkSheet, rowIndex, 0, 2, "平均水温:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 3, 4, num + "℃", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 2, "户内管材单位:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 3, 4, value2, this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 2, "入户装置局部系数:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 3, 4, this.option.ResistanceLocal.ToString(), this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 2, "入户装置局部阻力:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 3, 4, this.option.Resistance + "kPa", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 2, "压差控制阀位置:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 3, 4, value, this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 2, "压差控制阀正常工作最小压力差:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 3, 4, this.option.DiffDPValve + "kPa", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 2, "温控阀正常工作最小压力差:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 3, 4, this.option.DiffTCValve + "kPa", this.leftFormat);
			rowIndex += 3;
		}

		private void ExportCalcData(int rowIndex)
		{
			this.ExportText(this.bkSheet, rowIndex, 0, 1, "设计软件:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 2, 4, "盈建科机电 for Revit 2014-2016", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 1, "计算时间:", this.rightFormat);
			string value = DateTime.Now.ToString();
			this.ExportText(this.bkSheet, rowIndex, 2, 4, value, this.leftFormat);
		}

		private void ExportCirclePipe(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				for (int j = 0; j < shareRPipeSystem.FloorSystems.Count; j++)
				{
					FloorSystem floorSystem = shareRPipeSystem.FloorSystems[j];
					for (int k = 0; k < floorSystem.IndoorNum; k++)
					{
						IndoorSystem indoorSystem = floorSystem.IndoorSystems[k];
						string value = string.Format("共用立管[{0}]楼层[{1}]户内系统[{2}]水力计算表", i + 1, j + 1, k + 1);
						this.ExportText(this.bkSheet, rowIndex, 0, 12, value, this.nameTableFormat);
						rowIndex++;
						int num = 0;
						this.bkSheet.writeStr(rowIndex, num, "编号", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "负荷(W)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "G(kg/h)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "公称外径", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "长度(m)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "V(m/s)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "R(Pa/m)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "Pd(Pa)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "Σξ", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "ΔPy(Pa)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "ΔPj(Pa)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "ΔP(Pa)", this.tableFormat);
						num++;
						this.bkSheet.writeStr(rowIndex, num, "不平衡率%", this.tableFormat);
						rowIndex++;
						this.ExportCircle(ref rowIndex, indoorSystem.CirclePipes);
						rowIndex++;
					}
				}
			}
		}

		private void ExportCircle(ref int rowIndex, IList<CirclePipe> listCircle)
		{
			for (int i = 0; i < listCircle.Count; i++)
			{
				CirclePipe circlePipe = listCircle[i];
				int num = 0;
				this.bkSheet.writeStr(rowIndex, num, "环路" + (i + 1), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.Load, 0), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.Flow, 0), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, "De" + circlePipe.PipeData.OutDiameter, this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.PipeData.Length, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.PipeData.Speed, 3), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.PipeData.ResistanceFrict, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.PipeData.PressDynamic, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.ResCoefLocal, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.PipeData.DragForce, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.PipeData.ResistanceLocal, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.PipeData.Resistance, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(circlePipe.RateUnbalance, 2), this.tableFormat);
				rowIndex++;
			}
		}

		private void ExportRiserPipe(ref int rowIndex)
		{
			for (int i = 0; i < this.buildSystem.SrpSystems.Count; i++)
			{
				ShareRPipeSystem shareRPipeSystem = this.buildSystem.SrpSystems[i];
				string tableName = string.Format("共用立管[{0}]水力计算表", i + 1);
				this.ExportMainPipe(ref rowIndex, tableName, shareRPipeSystem.SupplyPipes, shareRPipeSystem.ReturnPipes);
			}
		}

		private void ExportOption(ref int rowIndex)
		{
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "设计软件:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 6, 8, "盈建科机电 for Revit 2014-2016", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "计算时间:", this.rightFormat);
			string value = DateTime.Now.ToString();
			this.ExportText(this.bkSheet, rowIndex, 6, 8, value, this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "室内供回干管形式:", this.rightFormat);
			string value2 = "同程";
			if (this.option.MainPipeInter == 2)
			{
				value2 = "异程";
			}
			else if (this.option.MainPipeInter == 1)
			{
				value2 = "同程";
			}
			this.ExportText(this.bkSheet, rowIndex, 6, 8, value2, this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "室内公用立管形式:", this.rightFormat);
			if (this.option.RiserPipeInter == 2)
			{
				value2 = "异程";
			}
			else if (this.option.RiserPipeInter == 1)
			{
				value2 = "同程";
			}
			this.ExportText(this.bkSheet, rowIndex, 6, 8, value2, this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "采暖形式:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 6, 8, "地热盘管采暖", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "供水温度:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 6, 8, this.option.TemperSupply + "℃", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "回水温度:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 6, 8, this.option.TemperReturn + "℃", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "系统总负荷:", this.rightFormat);
			this.ExportText(this.bkSheet, rowIndex, 6, 8, this.buildSystem.TotalLoad + "W", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "系统总流量:", this.rightFormat);
			string str = StringTools.DoubleToString(this.buildSystem.TotalFlow, 0);
			this.ExportText(this.bkSheet, rowIndex, 6, 8, str + "kg/h", this.leftFormat);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 3, 5, "系统总压降:", this.rightFormat);
			string str2 = StringTools.DoubleToString(this.buildSystem.TotalResistance, 2);
			this.ExportText(this.bkSheet, rowIndex, 6, 8, str2 + "Pa", this.leftFormat);
			rowIndex += 2;
		}

		private void ExportMainPipe(ref int rowIndex, string tableName, IList<HYPipeData> supplyPipe, IList<HYPipeData> returnPipe)
		{
			this.ExportText(this.bkSheet, rowIndex, 0, 12, tableName, this.nameTableFormat);
			rowIndex++;
			int num = 0;
			this.bkSheet.writeStr(rowIndex, num, "编号", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "负荷(W)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "G(kg/h)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "公称直径", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "长度(m)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "V(m/s)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "R(Pa/m)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "Pd(Pa)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "Σξ", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "ΔPy(Pa)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "ΔPj(Pa)", this.tableFormat);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "ΔP(Pa)", this.tableFormat);
			rowIndex++;
			this.ExportPipe(ref rowIndex, "供水", supplyPipe);
			this.ExportPipe(ref rowIndex, "回水", returnPipe);
			rowIndex++;
		}

		private void ExportPipe(ref int rowIndex, string identName, IList<HYPipeData> listPipe)
		{
			for (int i = 0; i < listPipe.Count; i++)
			{
				HYPipeData hypipeData = listPipe[i];
				int num = 0;
				this.bkSheet.writeStr(rowIndex, num, identName + (i + 1), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.Load, 0), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.Flow, 0), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString((double)hypipeData.Diameter, 0), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.Length, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.Speed, 3), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.ResistanceFrict, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.PressDynamic, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.ResCoefLocal, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.DragForce, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.ResistanceLocal, 2), this.tableFormat);
				num++;
				this.bkSheet.writeStr(rowIndex, num, StringTools.DoubleToString(hypipeData.Resistance, 2), this.tableFormat);
				rowIndex++;
			}
		}

		private void ExportHeader(ref int rowIndex, Sheet sheet, int colStart, int colEnd, string value)
		{
			sheet.setMerge(rowIndex, rowIndex, colStart, colEnd);
			sheet.writeStr(rowIndex, colStart, value, this.titleFormat);
			rowIndex += 2;
		}

		private void ExportText(Sheet sheet, int rowIndex, int colStart, int colEnd, string value, Format format)
		{
			sheet.setMerge(rowIndex, rowIndex, colStart, colEnd);
			for (int i = colStart; i <= colEnd; i++)
			{
				sheet.setCellFormat(rowIndex, i, format);
			}
			sheet.writeStr(rowIndex, colStart, value);
		}

		private void ExportTextNew(int rowIndex, int colStart, int colEnd, string value, Sheet sheet, Format format)
		{
			sheet.setMerge(rowIndex, rowIndex, colStart, colEnd);
			sheet.writeStr(rowIndex, colStart, value);
			for (int i = colStart; i <= colEnd; i++)
			{
				sheet.setCellFormat(rowIndex, i, format);
			}
		}

		private void ExportTextNew(Sheet sheet, int rowIndex, int colStart, int colEnd, string value, Format format)
		{
			sheet.setMerge(rowIndex, rowIndex, colStart, colEnd);
			sheet.writeStr(rowIndex, colStart, value, format);
		}

		private void ExportText(Sheet sheet, string value, int rowIndex, int endRowIndex, int colNum, Format format)
		{
			sheet.setMerge(rowIndex, endRowIndex, colNum, colNum);
			for (int i = rowIndex; i <= endRowIndex; i++)
			{
				sheet.setCellFormat(i, colNum, format);
			}
			sheet.writeStr(rowIndex, colNum, value);
		}

		private void ExportTextNew(Sheet sheet, string value, int rowIndex, int endRowIndex, int colNum, Format format)
		{
			sheet.setMerge(rowIndex, endRowIndex, colNum, colNum);
			for (int i = rowIndex; i <= endRowIndex; i++)
			{
				sheet.setCellFormat(i, colNum, format);
			}
			sheet.writeStr(rowIndex, colNum, value);
		}

		private void InitialLibxlFormat()
		{
			libxl.Font font = this.book.addFont();
			font.size = 10;
			this.nameTableFormat = this.book.addFormat();
			this.nameTableFormat.alignH = AlignH.ALIGNH_CENTER;
			this.nameTableFormat.alignV = AlignV.ALIGNV_CENTER;
			this.nameTableFormat.font = font;
			this.tableFormat = this.book.addFormat(this.nameTableFormat);
			this.tableFormat.setBorder(BorderStyle.BORDERSTYLE_THIN);
			this.leftFormat = this.book.addFormat(this.tableFormat);
			this.leftFormat.alignH = AlignH.ALIGNH_LEFT;
			this.rightFormat = this.book.addFormat(this.tableFormat);
			this.rightFormat.alignH = AlignH.ALIGNH_RIGHT;
			libxl.Font font2 = this.book.addFont();
			font2.size = 12;
			font2.name = "黑体";
			this.titleFormat = this.book.addFormat(this.nameTableFormat);
			this.titleFormat.font = font2;
		}

		private InBuildSystem buildSystem;

		private OptionData option;

		private Application excelApplication;

		private Workbook workbook;

		private Worksheet sheetSystemCalc;

		private Worksheet sheetCoefficient;

		private int rate;

		private Book book;

		private Sheet bkSheet;

		private BackgroundWorker backWorker;

		private Format titleFormat;

		private Format tableFormat;

		private Format nameTableFormat;

		private Format leftFormat;

		private Format rightFormat;

		private enum Align
		{
			Center,
			Right,
			Left
		}
	}
}
